package com.example.demo.service.impl;

import com.example.demo.common.Result;
import com.example.demo.entity.OrderTable;
import com.example.demo.mapper.BuyTicketMapper;
import com.example.demo.service.OrderService;
import com.example.demo.util.JWTUtil;
import com.example.demo.vo.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private BuyTicketMapper mapper;
    @Override
    public Result getOrders(String token) {
        //获取用户id
        token = token.substring(7);
        Integer userId = JWTUtil.parseToken(token).getId(); //令牌中含有id和name
        List<OrderTable> orders = mapper.selectOrder(userId);
        ResponseData responseData = new ResponseData();
        responseData.setTotal(orders.size());
        responseData.setList(orders);
        return Result.success(responseData);
    }

    @Override
    public Result cancelOrder(Integer id) {
        int res = mapper.cancelOrder(id);
        if (res == 1) {
            return Result.success();
        }else{
            return Result.fail();
        }
    }
}
